package com.dsunsoft.rtu.common.util;

import com.dsunsoft.rtu.common.constant.SecurityConstants;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 用户相关工具类
 */
public class JwtUtil {
    private static Logger logger = LoggerFactory.getLogger(JwtUtil.class);

    /**
     * 根据用户请求中的token 获取用户id
     *
     * @param request Request
     * @return "" userid
     */
    public static String getUserId(HttpServletRequest request) {
        String jwtkey = SpringContextHolder.getJwtSigningKey();
        Claims claims = buildClaims(getToken(request), jwtkey);
        if (null == claims)
            return "";
        return claims.get(SecurityConstants.JWT_USER_NAME).toString();
    }

    /**
     * 根据用户请求中的token map
     *
     * @param request
     * @return
     */
    public static Map<String, Object> getMap(HttpServletRequest request) {
        String jwtkey = SpringContextHolder.getJwtSigningKey();
        Claims claims = buildClaims(getToken(request), jwtkey);
        if (null == claims)
            return new HashMap<>();
        Map<String, Object> expectedMap = new HashMap<>(claims);
        return expectedMap;
    }

    /**
     * 根据请求heard中的token获取用户角色
     *
     * @param httpServletRequest request
     * @return 角色名
     */
    public static List<String> getRole(HttpServletRequest httpServletRequest) {
        String jwtkey = SpringContextHolder.getJwtSigningKey();
        return getRole(getToken(httpServletRequest), jwtkey);
    }

    /**
     * 获取请求中token
     */
    public static String getToken(HttpServletRequest httpServletRequest) {
        String authorization = httpServletRequest.getHeader(SecurityConstants.REQ_HEADER);
        return buildToken(authorization);
    }

    /**
     * jwt 解密
     */
    private static Claims buildClaims(String token, String jwtkey) {
        if (StringUtils.isBlank(token) || StringUtils.isBlank(jwtkey))
            return null;

        String key = "";
        try {
            key = Base64.getEncoder().encodeToString(jwtkey.getBytes());
            Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
            return claims;
        } catch (Exception ex) {
            logger.error("用户TOKEN解析异常,token:{},key:{}", token, key);
        }
        return null;
    }

    /**
     * 根据请求heard中的token获取用户角色
     *
     * @return 角色名
     */
    @SuppressWarnings("unchecked")
    private static List<String> getRole(String token, String jwtkey) {
        Claims claims = buildClaims(buildToken(token), jwtkey);
        if (null == claims)
            return new ArrayList<String>();

        List<String> roleCodes = (List<String>) claims.get(SecurityConstants.JWT_USER_AUTHORITIES);
        return roleCodes;
    }

//	/**
//	 * 获取Token信息
//	 *
//	 * @param httpServletRequest
//	 * @return
//	 */
//	public static TokenInfo getTokenInfo(HttpServletRequest httpServletRequest) {
//		TokenInfo tokenInfo = new TokenInfo();
//		String authorization = httpServletRequest.getHeader(SecurityConstants.REQ_HEADER);
//		String token = buildToken(authorization);
//		String signKey = SpringContextHolder.getJwtSigningKey();
//		Claims claims = buildClaims(token, signKey);
//		if (null != claims) {
//			tokenInfo.setUserId(String.valueOf(claims.get(SecurityConstants.JWT_USER_ID)));
//			tokenInfo.setUserName(String.valueOf(claims.get(SecurityConstants.JWT_USER_NAME)));
//			tokenInfo.setOfficeId(claims.get(SecurityConstants.JWT_USER_OFFICE_ID) == null?null:String.valueOf(claims.get(SecurityConstants.JWT_USER_OFFICE_ID)));
//			tokenInfo.setUserType(claims.get(SecurityConstants.JWT_USER_TYPE) == null?null:String.valueOf(claims.get(SecurityConstants.JWT_USER_TYPE)));
//
//		}
//		return tokenInfo;
//	}

    /**
     * buildToken
     *
     * @param token
     * @return
     */
    private static String buildToken(String token) {
        if (StringUtils.isBlank(token))
            return null;
        if (!token.contains(SecurityConstants.TOKEN_SPLIT))
            return token;
        return StringUtils.substringAfter(token, SecurityConstants.TOKEN_SPLIT);
    }
}
